package com.util;

import com.core.framework.spring.appcontext.SpringAppContext;

/**
 * AppUtil帮助类
 * @author 刘涛
 * @since Jun 7, 2012
 */
public class AppUtils {
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//		//SpringAppContext.getInstance().getAppContext().getBeanFactory().registerResolvableDependency(TestAction.class, "testAction");
//		String serviceName = "Test.JStruts2Test.web.TestAction";
//		DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) SpringAppContext.getInstance().getAppContext().getBeanFactory();
//		MutablePropertyValues properties = new MutablePropertyValues();
//		properties.addPropertyValue("testAction", serviceName + "Dao, autoLimit, autoCount");
//		Class<?> appServiceClass =(Class<?>) Class.forName(serviceName);
//		RootBeanDefinition definition = new RootBeanDefinition(appServiceClass, properties);
//
//		beanFactory.registerBeanDefinition("testAction", definition);
//	//	SpringAppContext.getInstance().getAppContext().getBeanFactory().autowireBean(TestAction.class);
//		//BeanFactory beanFactory = SpringAppContext.getInstance().getAppContext().getBeanFactory().r;
//		//BeanDefinition s ;
//		//SpringAppContext.getInstance().getAppContext().getBeanFactory().registerResolvableDependency(TestAction.class, "testAction");
//		TestAction testAction =AppUtils.getBean(TestAction.class) ;
//		testAction.toIndex();
		
	}
	
	/**
	 * 在任意类中注入类  默认通过名称注入并检测内部的注入关系
	 * @param beanClass 要注入的类
	 * @return
	 */
	public static Object autowiredBean(Class<?> beanClass )
	{
		return SpringAppContext.getInstance().getAppContext().getAutowireCapableBeanFactory().autowire(beanClass, 1, true);
	}
	/**
	 * 在任意类中注入类
	 * @param beanClass 要注入的类
	 * @param dependencyCheck
	 * @return
	 */
	public Object autowiredBean(Class<?> beanClass , boolean dependencyCheck)
	{
		return SpringAppContext.getInstance().getAppContext().getAutowireCapableBeanFactory().autowire(beanClass, 1, dependencyCheck);
	}
	/**
	 * 在任意类中注入类
	 * @param beanClass 要注入的类
	 * @param autowireMode 类型：{ 	int AUTOWIRE_NO = 0;  	int AUTOWIRE_BY_NAME = 1;  	int AUTOWIRE_BY_TYPE = 2; 	int AUTOWIRE_CONSTRUCTOR = 3;}
	 * @param dependencyCheck
	 * @return
	 */
	public Object autowiredBean(Class<?> beanClass , int autowireMode, boolean dependencyCheck)
	{
		return SpringAppContext.getInstance().getAppContext().getAutowireCapableBeanFactory().autowire(beanClass, autowireMode, dependencyCheck);
	}
	/**
	 * 获取任意一个类的实例 
	 * @param <T>
	 * @param clazz  
	 * @return  T
	 */
	public static <T> T getBean(Class<T> clazz)
	{
		return SpringAppContext.getInstance().getAppContext().getBean(clazz);
	}
	/**
	 * <p> 获取一个类的实例，规则如下,鉴于Jdk1.5以后的注解方式,Spring 注解注入以后则系统会统一对注入的对象生成一个KEY</p>
	 * <p> 生成KEY的规则是:注入类的名称，并且首字母小写 。 举例{注入类：TestService  生成Key:testService} </p>
	 * @param <T>
	 * @param beanName  beanName    [KEY]
	 * @param clazz      对象类
	 * @return  T
	 */
	public static <T> T getBean(String beanName ,Class<T> clazz)
	{
		return SpringAppContext.getInstance().getAppContext().getBean(beanName,clazz);
	}
	/**
	 * 通过beanName 获取类对象
	 * @param beanName
	 * @return Object
	 */
	public static Object getBean(String beanName )
	{
		return SpringAppContext.getInstance().getAppContext().getBean(beanName);
	}
	
}
